package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.excpt.enums.ExceptionMsgEnum;
import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.dao.UserInviteMotionRecordDao;
import cn.jcby.app.lebu.entity.UserInviteMotionRecordEntity;
import cn.jcby.app.lebu.enums.MessageTypeEnum;
import cn.jcby.app.lebu.service.UserInviteMotionRecordService;
import cn.jcby.app.lebu.service.UserMessageService;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.UserInfoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userInviteMotionRecordService")
public class UserInviteMotionRecordServiceImpl extends ServiceImpl<UserInviteMotionRecordDao, UserInviteMotionRecordEntity> implements UserInviteMotionRecordService {

    @Autowired
    private UserMessageService messageService;
    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<UserInviteMotionRecordEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<UserInviteMotionRecordEntity>lambdaQuery()
        );

        return new PageUtils(page);
    }
    @Override
    public void add(String inviteUserId){
        Assert.isFalse(UserInfoUtil.getUserId().equals(inviteUserId), ExceptionMsgEnum.USER_CAN_NOT_INVITE_SELF.getCode()+"");
        UserInviteMotionRecordEntity one = getOne(Wrappers.<UserInviteMotionRecordEntity>lambdaQuery()
                .eq(UserInviteMotionRecordEntity::getInvitedUserId, inviteUserId)
                .eq(UserInviteMotionRecordEntity::getUserId, UserInfoUtil.getUserId()));
        if (one==null) {
            UserInviteMotionRecordEntity userInviteMotionRecord = new UserInviteMotionRecordEntity();
            userInviteMotionRecord.setInvitedUserId(inviteUserId);
            userInviteMotionRecord.setUserId(UserInfoUtil.getUserId());
            save(userInviteMotionRecord);
            //添加用户日志
            messageService.addRecord(UserInfoUtil.getUserId(),inviteUserId, MessageTypeEnum.INVITE_TA_PUBLISH) ;
        }
    }

    @Override
    public void updateBy(UserInviteMotionRecordEntity userInviteMotionRecordReq){
        UserInviteMotionRecordEntity userInviteMotionRecord = new UserInviteMotionRecordEntity() ;
        updateById(userInviteMotionRecord) ;
    }
}